package com.heaven.service;

import java.sql.SQLException;
import java.util.List;

import com.heaven.dao.AccountDao;
import com.heaven.model.Account;

public class AccountService {
	private AccountDao ad = new AccountDao();
	// 입금 실행
	// account1 : 계좌 정보
	// money : 입금액
	public Account deposit(int accountNo, int money) throws ClassNotFoundException, SQLException {
		Account account = ad.select(accountNo);
		
		account.setBalance(account.getBalance() + money);
		
		ad.update(account);
			
		return account;
	}	

	// 출금
	// account1 : 계좌 정보
	// money : 출금액
	public void withdraw(Account account1, int money) throws Exception {
		int balance = account1.getBalance();
		
		if(balance >= money) {
			account1.setBalance(balance - money);
		} else {
			throw new Exception("잔액이 부족합니다.");
		}
	}

	// 이체
	// account1 은 출금 계좌
	// account2 은 입금 계좌
	// money 이체액
	public void transfer(Account account1, Account account2, int money) throws Exception {
		//int balance1 = account1.getBalance();
		//int balance2 = account2.getBalance();
		
		//account1.setBalance(balance1 - money);
		//account2.setBalance(balance2 + money);

		//withdraw(account1, money);
		//deposit(account2, money);
	}
	
	public List<Account> list() throws ClassNotFoundException, SQLException {
		return ad.selectAll();
	}

	public Account detail(int accountNo) throws ClassNotFoundException, SQLException {		
		return ad.select(accountNo);
	}
}